from flask import session, jsonify
from flask_restful import Resource

from database import Database
from users.book.model.Stack import Stack


# 创建数据库连接
class GetAccount(Resource):
    def get(self):
        username = session.get('username')
        db = Database()

        try:
            accountList = db.execute_query("SELECT * FROM useraccount WHERE userId = %s", (username,))
            account = None
            if accountList.__len__() > 0:
                account = accountList[0]
            if account:
                line_books = db.execute_query("SELECT * FROM linebook WHERE userId = %s AND status = %s", (username, "borrowed"))
                list_books_json = []

                if line_books:
                    for line_book in line_books:
                        book = db.execute_query("SELECT * FROM book WHERE bookId = %s", (line_book[3],))[0]
                        stack = db.execute_query("SELECT * FROM stack WHERE stackId = %s", (book[3],))[0]

                        stack_json = {
                            "stackId": stack[0],
                            "categoryId": stack[1],
                            "imageUrl": stack[2],
                            "title": stack[3],
                            "description": stack[4],
                            "author": stack[5],
                            "status": stack[6],
                            "quantity": str(stack[7])
                        }
                        book_data = {
                            "bookId": book[0],
                            "categoryId": book[1],
                            "imageUrl": book[2],
                            "stackId": book[3],
                            "ownerId": book[4],
                            "condition": book[5],
                            "fee": str(book[6]),
                            "price": str(book[7]),
                            "status": book[8],
                            "LineBookStatus": line_book[2],
                            "libraryAddress": line_book[4],
                            "borrowedTill": str(line_book[5]),
                            "returnAfter": str(line_book[6]),
                            "dayToken": str(line_book[7]),
                            "stack": stack_json
                        }
                        list_books_json.append(book_data)
                account = {
                    "userId": account[0],
                    "type": account[1],
                    "borrowLimit": account[2],
                    "password": account[3],
                    "firstName": account[4],
                    "lastName": account[5],
                    "email": account[6],
                    "address": account[7],
                    "imageUrl": account[8],
                    "listBooks": list_books_json
                }
                return {"account": account}, 200
            else:
                return {"message": "server internal error"}, 401
        finally:
            db.close_connection()